package com.example.xunua_mymusic;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.xunua_mymusic.entitys.UserBean;
import com.example.xunua_mymusic.utils.MySqliteHelper;
import com.example.xunua_mymusic.utils.Sqlite_utils;

public class RegisterActivity extends AppCompatActivity {
    Button btn_register;
    ImageView img_back;
    EditText et_name,et_pwd,et_iphone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        et_name=findViewById(R.id.register_et_name);
        et_pwd=findViewById(R.id.register_et_pwd);
        et_iphone=findViewById(R.id.register_et_iphone);
        btn_register=findViewById(R.id.register_btn_register);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=et_name.getText().toString().trim();
                String pwd=et_pwd.getText().toString().trim();
                String iphone=et_iphone.getText().toString().trim();
                if (name.isEmpty() || pwd.isEmpty() || iphone.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请不要留空", Toast.LENGTH_SHORT).show();
                    return;
                }
                MySqliteHelper mySqliteHelper=new MySqliteHelper(RegisterActivity.this,1);
                Sqlite_utils sqlite_utils=new Sqlite_utils();
                String s = sqlite_utils.checkNmae(mySqliteHelper, name);
                if ("此用户名不存在".equals(s)){
                    SQLiteDatabase writableDatabase = mySqliteHelper.getWritableDatabase();
                    writableDatabase.execSQL("insert into user(name,password,iphone)values (?,?,?)",new Object[]{name,pwd,iphone});
                    writableDatabase.close();
                    Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                    UserBean bean=new UserBean();
                    bean.setName(name);
                    bean.setPassword(pwd);
                    intent.putExtra("bean",bean);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(RegisterActivity.this, "用户已被注册！", Toast.LENGTH_SHORT).show();
                }
            }
        });

        img_back=findViewById(R.id.register_img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
